package org.yjh.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.yjh.dao.UserDao;
import org.yjh.pojo.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class intercepotorLogin implements HandlerInterceptor {

    @Autowired
    private UserDao userDao;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("在目标方法执行之前，执行");
        String uid = request.getParameter("uid");
        String ucookie = request.getParameter("ucookie");
        if(uid!=null && ucookie!=null){
            // 如果uid于ucookie不匹配 false
//            System.out.println("uid: "+Integer.parseInt(uid));
            // 数据库获取信息进行匹配
            User user = userDao.getUser(Integer.parseInt(uid));
            if(ucookie.equals(user.getUcookie())){
                System.out.println("放行");
                return true;  // 放行
            }
        }
        System.out.println("没有登录，不能进行");

        return false;


    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("在目标方法执行之后，视图对象返回之前执行");
        System.out.println("该方法可以对ModelAndView 进行修改");
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("在流程都执行完毕后，执行");
    }
}
